package com.example.supermarket.mapper;

import com.example.supermarket.entity.Member;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>会员映射表<p/>
 * @Author: porridge
 * @Date:2021/10/8 17:28
 */
@Repository
@Mapper
public interface MemberMapper {

    /**
     * <p>会员添加<p/>
     */
    @Insert("insert into member (card_id, name, password) values (#{cardId}, #{name}, #{password})")
    int insertMember(@Param("cardId") String cardId, @Param("name") String name, @Param("password") String password);

    /**
     * <p>根据id删除会员<p/>
     */
    @Delete("delete from member where id = #{id}")
    int deleteById(@Param("id") int id);

    /**
     * <p>根据id查找All全部<p/>
     */
    @Select("select id, card_id, name, password from member where id = #{id}")
    Member findById(@Param("id") int id);

    /**
     * <p>根据name查找All全部<p/>
     */
    @Select("select id, card_id, name, password from member where name = #{name} order by id desc")
    List<Member> findAllByName(@Param("name") String name);

    /**
     * <p>查找全部的会员<p/>
     */
    @Select("select id, card_id, name from member order by id desc")
    List<Member> findAll();

    /**
     * <p>查找会员单个信息<p/>
     */
    @Select("select id, card_id, name, password from member where name = #{name}")
    Member findInfoByName(@Param("name") String name);

    /**
     * <p>根据id修改会员卡号<p/>
     */
    @Update("update member set card_id = #{cardId} where id = #{id}")
    int updateCardIdById(@Param("id") int id, @Param("cardId") String cardId);

    /**
     * <p>id进行对会员密码的修改<p/>
     */
    @Update("update member set password=#{password} where id=#{id}")
    int updatePasswordById(@Param("id") int id, @Param("password") String password);

    /**
     * <p>通过id进行对会员昵称和会员卡进行修改<p/>
     */
    @Update("update member set name = #{name}, card_id = #{cardId} where id = #{id}")
    int updateNameAndCardIdById(@Param("id") int id, @Param("name") String name, @Param("cardId") String cardId);

    /**
     * <p>根据会员ID查找</p>
     */
    @Select("select id, name, card_id from member where card_id = #{cardId}")
    Member findByCardId(String cardId);
}
